package com.hmall.api.client.fallback;

import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.api.dto.OrderDetailDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 自定义FallbackFactory
 */
@Slf4j
public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {
    /**
     * 调用失败后的后备解决方案
     * @param cause
     * @return
     */
    @Override
    public ItemClient create(Throwable cause) {

        return new ItemClient() {
            @Override
            public List<ItemDTO> queryByItemIds(Collection<Long> ids) {
                log.error("查询商品失败！",cause);
                return Collections.emptyList(); //返回一个空集合
            }

            @Override
            public void deductStock(List<OrderDetailDTO> items) {
                throw new RuntimeException(cause);
            }

            @Override
            public void restoreStock(List<OrderDetailDTO> items) {

            }

            @Override
            public ItemDTO queryItemById(Long id) {
                return null;
            }
        };
    }
}
